<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Laraki
 * Date: 09/06/13
 * Time: 01:23
 * To change this template use File | Settings | File Templates.
 */

require_once '../utils/RssReader.php';
require_once '../utils/HTMLReader.php';
define('HOST','localhost');
define('USER','root');
define('PASSWD','');
define('DATA_BASE','marocnews');
require_once 'MySQLConnection.php';

//Categories
class Categorie
{
    const general = "general";
    const politique="politique";
    const divers="divers";
    const sport = "sport";
    // etc.
};

class NewsDAO
{

        private $db;

        public function __construct()
        {
            $this->db=new MySQLConnection();
        }

        public function refresh(){
            $feeds=RssReader::getfeeds();
            //Parcours des feeds
            foreach($feeds as $key=>$items){
                    if($key=='hespress') $this->refresh_hespressnews($items);
                    else if($key=='menara_actu') $this->refresh_menaranews($items,Categorie::general);
                    else if($key=='menara_tech') $this->refresh_menaranews($items,Categorie::divers);
                    else if($key=='menara_divers') $this->refresh_menaranews($items,Categorie::divers);
                    else if($key=='menara_economie') $this->refresh_menaranews($items,Categorie::divers);
                    else if($key=='menara_culture') $this->refresh_menaranews($items,Categorie::divers);
                    else if($key=='menara_sport') $this->refresh_menaranews($items,Categorie::sport);
                    //else if($key=='hibapress') $this->refresh_hibapressnews($items);
            }
        }

        //refresh feeds
        private function refresh_hespressnews($items){
            $categories=array(
                'societe'=>Categorie::general,
                'politique'=>Categorie::politique,
                'regions'=>Categorie::divers,
                'societe'=>Categorie::general,
                'faits_divers'=>Categorie::divers,
                '24-heures'=>Categorie::general
            );
            foreach($items as $item){
                $news=null;
                $url=$item['link'];
                if(preg_match("^http://hespress.com/(politique|regions|societe|faits_divers|24-heures)/.+.html^",$url,$matches) && !$this->link_exist($url) ) $news = HTMLReader::ExtractHespressNews($url,$categories[$matches[1]]);
                if($news==null) continue;
                $this->save($news);
            }
        }
        private function refresh_menaranews($items,$categ){
            foreach($items as $item){
                $news=null;
                $url=$item['link'];
                if(preg_match("^http://www.menara.ma/ar/node/.+^",$url) && !$this->link_exist($url)) $news= HTMLReader::ExtractMenaraNews($url,$categ);
                if($news==null) continue;
                $this->save($news);
            }
        }

        private function refresh_hibapressnews($items){
            foreach($items as $item){
                $news=null;
                $url=$item['link'];
                if(preg_match("^http://www.hibapress.com/.+.html^",$url) && !$this->link_exist($url)) $news = HTMLReader::ExtractHibapressNews($url);
                if($news==null) continue;
                $this->save($news);
            }
        }

        private function save($news){
            $_db=$this->db;
            $_db->connect();
            $r="insert into news VALUES('$news->link','$news->date','$news->categorie','$news->source','$news->source_icon','$news->photo','$news->title','$news->contenu')";
            $_db->executeQuery($r);
        }

        private function link_exist($url){
            $_db=$this->db;
            $_db->connect();
            $res=$_db->executeQuery("select * from news where link='$url'");
            if($_db->fetch_row_line($res)) return true;
            else return false;
        }

}
